package com.surent.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.GetCallback;
import com.hailong.surent.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.surent.model.dbmodel.Image;
import com.surent.model.viewmodel.HouseList;
import com.surent.view.custom.BaseAdapterHelper;
import com.surent.view.custom.QuickAdapter;

public class AdapterHouseList extends QuickAdapter<HouseList> {

	private DisplayImageOptions options;

	public AdapterHouseList(Context context) {
		super(context, R.layout.item_house);

		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher)// 加载中显示的图片
				.showImageForEmptyUri(R.drawable.ic_launcher)// Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.ic_launcher)// 图片加载/解码过程中错误时候显示的图片
				.cacheInMemory(true)// 是否允许入内容
				.cacheOnDisk(true)// 是否允许存入外存
				.considerExifParams(true).displayer(new RoundedBitmapDisplayer(5)).bitmapConfig(Bitmap.Config.RGB_565)// //设置图片的解码类型
				.build();
	}

	@Override
	protected void convert(final BaseAdapterHelper helper, final HouseList item) {
		helper.setText(R.id.tv_item_houselist_houseTitle, item.getTitle());
		helper.setText(R.id.tv_item_houselist_houseCityAndCommunity, item.getDistrict() + "—" + item.getCommunityName());
		helper.setText(R.id.tv_item_houselist_housePrice, item.getPrice());
		helper.setText(R.id.tv_item_house_houseLayoutEtc, item.getHouseLayout());
		
		helper.setText(R.id.tv_item_houselist_houseDate, item.getHouse().getCreatedAt().toGMTString());

		// TODO
		AVQuery<Image> query = AVQuery.getQuery(Image.class);
		query.equals(item.getHouse());
		query.getFirstInBackground(new GetCallback<Image>() {

			@Override
			public void done(Image image, AVException e) {
				if (e == null) {
					helper.setImageUrl(R.id.iv_item_houselist_houseIcon, image.getImageFile().getUrl(), options);
				}
			}
		});
	}
}
